package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"
	"regexp"
	"time"
	"web/crawl_movie/models"
)

type CrawlMovieController struct {
	beego.Controller
}

func (c *CrawlMovieController) CrawlMovie() {
	// 连接redis
	addr := beego.AppConfig.String("redisaddr")
	pwd := beego.AppConfig.String("redispwd")
	models.ConnectRedis(addr, pwd)
	// 设置起始url，并加入队列
	surl := "https://movie.douban.com"
	//surl := "https://movie.douban.com/subject/30318116/"
	resp := httplib.Get(surl)
	movie_html, err := resp.String()
	if err != nil {
		fmt.Println(err)
	}
	reg := regexp.MustCompile(`<a.*?href="(https://movie.douban.com/subject/.*?)/.*"`)
	result := reg.FindAllStringSubmatch(movie_html,-1)
	for _,url := range result{
		models.PutinQueue(url[1])
	}
	// 开启循环
	for {
		length := models.GetQueueLength()
		if length == 0 {
			break
		}
		surl = models.PopfromQueue()
		if models.IsVisit(surl) {
			continue
		}
		resp := httplib.Get(surl)
		movie_html, err := resp.String()
		if err != nil {
			fmt.Println(err)
		}
		var movieInfo models.MovieInfo
		movieInfo.Movie_name = models.GetMovieName(movie_html)
		if movieInfo.Movie_name != "" {
			movieInfo.Movie_id = models.GetMovieId(surl)
			movieInfo.Movie_director = models.GetMovieDirector(movie_html)
			movieInfo.Movie_country = models.GetMovieCountry(movie_html)
			movieInfo.Movie_grade = models.GetMovieGrade(movie_html)
			movieInfo.Movie_language = models.GetMovieLanguage(movie_html)
			movieInfo.Movie_main_character = models.GetMovieMainCharacters(movie_html)
			movieInfo.Movie_on_time = models.GetMovieOnTime(movie_html)
			movieInfo.Movie_pic = models.GetMoviePic(movie_html)
			movieInfo.Movie_span = models.GetMovieSpan(movie_html)
			movieInfo.Movie_type = models.GetMovieType(movie_html)
			movieInfo.Movie_writer = models.GetMovieWriter(movie_html)
			_, err := models.AddMovie(&movieInfo)
			if err != nil {
				beego.Error(err)
			}else {
				fmt.Printf("%s 爬取成功!\n", surl)
				//fmt.Println(movieInfo)
			}
		}
		// 提取该页面的所有连接
		urls := models.GetMovieUrls(movie_html)
		for _, url := range urls {
			models.PutinQueue(url)
			c.Ctx.WriteString("<br>" + url + "</br>")
		}
		models.AddToSet(surl)
		time.Sleep(time.Second)
	}
	c.Ctx.WriteString("爬取结束！")
}
